package com.enqualcomm.kids.mvp.map;

import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;


public class AnimationUtil {
	/**
	 * 泡泡弹出效果
	 * @param v
	 */
	public static void popShowAnimation(View v,float fromDegrees,float toDegrees ){
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1,
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF,0.5f);
		scaleAnimation.setDuration(400);
		
		RotateAnimation rotateAnimation	= new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 1);
		rotateAnimation.setDuration(0);
		
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setDuration(1000);
		
		AnimationSet as = new AnimationSet(true);
		as.setInterpolator(new OvershootInterpolator());
		as.setFillAfter(true);
		as.addAnimation(scaleAnimation);
		as.addAnimation(alphaAnimation);
		as.addAnimation(rotateAnimation);
		v.startAnimation(as);
	}
	/**
	 * infowindow翅膀展开的动画
	 */
	public static void wingShowAnimation(View v,boolean right){
		//翅膀限时隐藏的，在infowindow的主体弹出来之后才开始执行缩放动画
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1,
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF,0.5f);
		scaleAnimation.setDuration(400);
		
		TranslateAnimation translateAnimation = null;
		if (right) {
			translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, -1, 
					Animation.RELATIVE_TO_SELF, 0, 
					Animation.RELATIVE_TO_SELF, -1,
					Animation.RELATIVE_TO_SELF, 0);
		}else {
			translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 1, 
					Animation.RELATIVE_TO_SELF, 0, 
					Animation.RELATIVE_TO_SELF, -1,
					Animation.RELATIVE_TO_SELF, 0);
		}
		translateAnimation.setDuration(400);
		
		AnimationSet as = new AnimationSet(true);
		as.setInterpolator(new OvershootInterpolator());
		as.setFillAfter(true);
		as.setStartOffset(500);
		
		as.addAnimation(scaleAnimation);
		as.addAnimation(translateAnimation);
		v.startAnimation(as);
	}
	/**
	 * 翅膀隐藏的动画
	 * @param v
	 * @param right
	 */
	public static void wingHideAnimation(View v,boolean right){
		TranslateAnimation translateAnimation = null;
		if (right) {
			translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0, 
					Animation.RELATIVE_TO_SELF, -1, 
					Animation.RELATIVE_TO_SELF, 0,
					Animation.RELATIVE_TO_SELF, -1);
		}else {
			translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0, 
					Animation.RELATIVE_TO_SELF, 1, 
					Animation.RELATIVE_TO_SELF, 0,
					Animation.RELATIVE_TO_SELF, -1);
		}
		translateAnimation.setDuration(300);
		translateAnimation.setInterpolator(new AccelerateInterpolator());
		translateAnimation.setFillAfter(true);
		
		v.startAnimation(translateAnimation);
	}
	/**
	 * infowindow隐藏动画
	 * @param v
	 */
	public static void popHideAnimation(final View v,float fromDegrees,float toDegrees ){
		ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0, 1, 0,
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF,0.5f);
		scaleAnimation.setDuration(300);
		
		RotateAnimation rotateAnimation	= new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 1);
		rotateAnimation.setDuration(0);
		
		AnimationSet as = new AnimationSet(true);
		as.setInterpolator(new AccelerateInterpolator());
		as.setFillAfter(true);
		as.setStartOffset(400);
		as.addAnimation(scaleAnimation);
		as.addAnimation(rotateAnimation);
		as.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				v.clearAnimation();
				v.setVisibility(View.INVISIBLE);
			}
		});
		v.startAnimation(as);
	}

}
